package insta.project;

import insta.project.remote.HttpRequest;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class HomeActivity extends Activity implements ViewFactory,
		View.OnClickListener {

	private TextSwitcher[] mSwitcher;
	private String[] arrArtistNameList;

	private int mCounter = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);

		String artistList = "";
		try {
			artistList = HttpRequest
					.GET("http://www.freebangers.com/androidquery/artists");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (artistList.length() > 0) {
			arrArtistNameList = artistList.split(";");
		}
		mSwitcher = new TextSwitcher[3];
		loadTextSwitcher(0);
		loadTextSwitcher(1);
		loadTextSwitcher(2);
	}

	public void loadTextSwitcher(int id) {
		int textId = R.id.text_switcher_0;
		textId = (id == 1) ? R.id.text_switcher_1 : textId;
		textId = (id == 2) ? R.id.text_switcher_2 : textId;


		
		mSwitcher[id] = (TextSwitcher) findViewById(textId);

		mSwitcher[id].setFactory(this);
		Animation in = AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in);
		Animation out = AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out);
		mSwitcher[id].setInAnimation(in);
		mSwitcher[id].setOutAnimation(out);

		// Now run the loop
		TagNameUpdater tagNameUpdater = new TagNameUpdater();
		TextSwitcher tsTextContent = (TextSwitcher) findViewById(textId);
		tagNameUpdater.execute(tsTextContent, id);
	}

	public View makeView() {
		TextView t1 = new TextView(this);
		// t1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);

		Random rnd = new Random();
		int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),
				rnd.nextInt(256));
		t1.setTextColor(color);
		t1.setTextSize(rnd.nextInt(25) + 10);
		return t1;
	}

	private class TagNameUpdater extends AsyncTask<Object, Object, Void> {

		int textId;
		TextSwitcher tSwitcher;

		@Override
		protected Void doInBackground(Object... _textSwitcher) {
			TextSwitcher tsTextContent1 = (TextSwitcher) _textSwitcher[0];
			Integer value = (Integer) _textSwitcher[1];
			this.tSwitcher = tsTextContent1;
			this.textId = value;
			try {
				publishProgress(tsTextContent1, value);

				Random random = new Random();
				int randomTime = random.nextInt(1000) + 1000;
				Thread.sleep(randomTime);
			} catch (InterruptedException e) {
				Log.d("InterruptedException", e.getMessage());
			}
			return null;
		}

		/**
		 * Handle display changes
		 */
		protected void onProgressUpdate(Object... textContent) {
			mCounter++;
			Random random = new Random();
			int id = (Integer) textContent[1];
			int pick1 = random.nextInt(arrArtistNameList.length);
			mSwitcher[id].setText(arrArtistNameList[pick1]);

			TextView t1 = (TextView) mSwitcher[id].getChildAt(0);
			int color;
			Random rnd = new Random();
			color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),
					rnd.nextInt(256));
			t1.setTextColor(color);
			t1.setTextSize(rnd.nextInt(25) + 10);
		}

		protected void onPostExecute(Void result) {
			TagNameUpdater tagNameUpdater = new TagNameUpdater();
			tagNameUpdater.execute(this.tSwitcher, this.textId);
		}

	}

	public void start_button(View vView) {
		Intent intent = new Intent().setClass(this, AccueilActivity.class);
		this.startActivity(intent);
		this.finish();
	}

	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

}